<?php
#BEGIN_LICENSE
#-------------------------------------------------------------------------
# Module: CGBlog (c) 2010 by Robert Campbell 
#         (calguy1000@cmsmadesimple.org)
#  An addon module for CMS Made Simple to allow creation, management of
#  and display of blog articles.
# 
#-------------------------------------------------------------------------
# CMS - CMS Made Simple is (c) 2005 by Ted Kulp (wishy@cmsmadesimple.org)
# This project's homepage is: http://www.cmsmadesimple.org
#
#-------------------------------------------------------------------------
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# However, as a special exception to the GPL, this software is distributed
# as an addon module to CMS Made Simple.  You may not use this software
# in any Non GPL version of CMS Made simple, or in any version of CMS
# Made simple that does not indicate clearly and obviously in its admin 
# section that the site was built with CMS Made simple.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# Or read it online: http://www.gnu.org/licenses/licenses.html#GPL
#
#-------------------------------------------------------------------------
#END_LICENSE
$gCms = cmsms();

$smarty->assign('formstart',$this->CreateFormStart($id,'defaultadmin'));  
$sel_categories = '';
	
if (isset($params['submit_massdelete']) ) {
  if (!$this->CheckPermission('Delete CGBlog')) {
    echo $this->ShowErrors($this->Lang('needpermission', array('Modify CGBlog')));
  }
  else if( isset($params['sel']) && is_array($params['sel']) &&	count($params['sel']) > 0 ) {
    foreach( $params['sel'] as $cgblog_id ) {
      $this->delete_article( $cgblog_id );
    }
  }
}
if (isset($params['submitfilter'])) {
  if( isset($params['categories']) ) $sel_categories = $params['categories'];
}
	
$categorylist = array();
{
  $query = "SELECT * FROM ".cms_db_prefix()."module_cgblog_categories ORDER BY sort_order";
  $dbresult = $db->Execute($query);
  while ($dbresult && $row = $dbresult->FetchRow()) {
    $categorylist[$row['id']] = $row['name'];
  }
  $dbresult->Close();
}

if( isset( $params['sortby'] ) ) $this->SetPreference('article_sortby',str_replace("'",'_',$params['sortby']));
if( isset( $params['pagelimit'] ) ) $this->SetPreference('article_pagelimit',(int)$params['pagelimit']);
$pagelimit = $this->GetPreference('article_pagelimit',25);
$pagenumber = 1;
if( isset( $params['pagenumber'] ) ) $pagenumber = $params['pagenumber'];
$startelement = ($pagenumber-1) * $pagelimit;
$sortby = 'cgblog_date DESC'; $tablesorter = '[[3,1]]';
$esortby = $this->GetPreference('article_sortby','cgblog_date DESC:[[3,1]]');
//$sortby = 'cgblog_date DESC:[[3,1]]';
if( strpos($esortby,':') !== FALSE ) list($sortby,$tablesorter) = explode(':',$esortby,2);
$sortlist = array();
$sortlist[$this->Lang('post_date_desc')]='cgblog_date DESC:[[3,1]]';
$sortlist[$this->Lang('post_date_asc')]='cgblog_date ASC:[[3,0]]';
$sortlist[$this->Lang('expiry_date_desc')]='end_time DESC:[[5,1]]';
$sortlist[$this->Lang('expiry_date_asc')]='end_time ASC:[[5,0]]';
$sortlist[$this->Lang('title_asc')] = 'cgblog_title ASC:[[0,0]]';
$sortlist[$this->Lang('title_desc')] = 'cgblog_title DESC:[[0,1]]';

$smarty->assign('tablesorter',$tablesorter);
$smarty->assign('categorylist',$categorylist);
$smarty->assign('sel_categories',$sel_categories);
$smarty->assign('prompt_sorting',$this->Lang('prompt_sorting'));
$smarty->assign('input_sorting',$this->CreateInputDropdown($id,'sortby',$sortlist,-1,$esortby));
$smarty->assign('submitfilter',$this->CreateInputSubmit($id,'submitfilter',$this->Lang('submit')));
$smarty->assign('prompt_pagelimit',$this->Lang('prompt_pagelimit'));
$smarty->assign('input_pagelimit',$this->CreateInputDropdown($id,'pagelimit',
							     array('5'=>5,'25'=>25,'50'=>50,
								   '100'=>100,'500'=>500,'1000'=>1000,$this->Lang('unlimited')=>999999999),
							     -1,$pagelimit));
$smarty->assign('formend',$this->CreateFormEnd());

//	
//Load the matching articles
//
$entryarray = array();
$dbresult = '';
$query1 = "SELECT n.* FROM ".cms_db_prefix()."module_cgblog n";
$query2 = "SELECT count(n.cgblog_id) AS count FROM ".cms_db_prefix()."module_cgblog n";
$parms = array();
$joins = array();
if (is_array($sel_categories) && count($sel_categories)) {
  $query1 .= ' LEFT OUTER JOIN '.cms_db_prefix().'module_cgblog_blog_categories bc ON n.cgblog_id = bc.blog_id';
  $query2 .= ' LEFT OUTER JOIN '.cms_db_prefix().'module_cgblog_blog_categories bc ON n.cgblog_id = bc.blog_id';
  $query1 .= " WHERE bc.category_id IN (".implode(',',$sel_categories).')';
  $query2 .= " WHERE bc.category_id IN (".implode(',',$sel_categories).')';
}
$query1 .= ' ORDER by '.$sortby;

// if is done to help adodb.
$numrows = -1;
$dbresult = $db->SelectLimit( $query1, $pagelimit, $startelement, $parms);
$row = $db->GetRow($query2,$parms);
$numrows = $row['count'];

$pagecount = (int)($numrows/$pagelimit);
if( ($numrows % $pagelimit) != 0 ) $pagecount++;
// some pagination variables to smarty.
if( $pagenumber == 1 ) {
  $smarty->assign('prevpage','<');
  $smarty->assign('firstpage','<<');
 }
else {
  $smarty->assign('prevpage', $this->CreateLink($id,'defaultadmin', $returnid,'<', 
						array('pagenumber'=>$pagenumber-1,'active_tab'=>'articles')));
  $smarty->assign('firstpage',$this->CreateLink($id,'defaultadmin',$returnid,'<<',
						array('pagenumber'=>1,'active_tab'=>'articles')));
}
if( $pagenumber >= $pagecount ) {
  $smarty->assign('nextpage','>');
  $smarty->assign('lastpage','>>');
}
else {
  $smarty->assign('nextpage', $this->CreateLink($id,'defaultadmin', $returnid,'>',
						array('pagenumber'=>$pagenumber+1, 'active_tab'=>'articles')));
  $smarty->assign('lastpage', $this->CreateLink($id,'defaultadmin', $returnid,'>>',
						array('pagenumber'=>$pagecount,'active_tab'=>'articles')));
}
$smarty->assign('pagenumber',$pagenumber);
$smarty->assign('pagecount',$pagecount);
$smarty->assign('oftext',$this->Lang('prompt_of'));
$rowclass = 'row1';

$admintheme =& $gCms->variables['admintheme'];
while ($dbresult && $row = $dbresult->FetchRow()) {
  $onerow = new stdClass();

  $onerow->id = $row['cgblog_id'];
  $onerow->url = $row['url'];
  $onerow->author = $row['author'];
  $onerow->title = $this->CreateLink($id, 'admin_editarticle', $returnid, $row['cgblog_title'], array('articleid'=>$row['cgblog_id']));
  $onerow->data = $row['cgblog_data'];
  $onerow->expired = 0;
  $onerow->postdate = $row['cgblog_date'];
  $onerow->startdate = $row['start_time'];
  $onerow->enddate = $row['end_time'];
  $onerow->u_postdate = $db->UnixTimeStamp($row['cgblog_date']);
  $onerow->u_startdate = $db->UnixTimeStamp($row['start_time']);
  $onerow->u_enddate = $db->UnixTimeStamp($row['end_time']);
  $onerow->o_status = $row['status'];
  if( $onerow->u_enddate > 0 && $onerow->u_enddate < time() ) $onerow->expired = 1;
  $onerow->status = $this->Lang($row['status']);
  if( $this->CheckPermission('Approve CGBlog') ) {
    if( $row['status'] == 'published' ) {
      $onerow->approve_link = $this->CreateLink($id,'approvearticle',
						$returnid,
						$admintheme->DisplayImage('icons/system/true.gif',$this->Lang('revert'),'','','systemicon'),array('approve'=>0,'articleid'=>$row['cgblog_id']));
    }
    else {
      $onerow->approve_link = $this->CreateLink($id,'approvearticle',
						$returnid,
						$admintheme->DisplayImage('icons/system/false.gif',$this->Lang('approve'),'','','systemicon'),array('approve'=>1,'articleid'=>$row['cgblog_id']));
    }
  }
  $onerow->rowclass = $rowclass;
    
  $onerow->select = $this->CreateInputCheckbox($id,'sel[]',$row['cgblog_id']);
  if( $this->CheckPermission('Modify CGBlog') ) {
    $onerow->editlink = $this->CreateLink($id, 'admin_editarticle', $returnid, $gCms->variables['admintheme']->DisplayImage('icons/system/edit.gif', $this->Lang('edit'),'','','systemicon'), array('articleid'=>$row['cgblog_id']));
  }
  if( $this->CheckPermission('Delete CGBlog') ) {
    $onerow->deletelink = $this->CreateLink($id, 'deletearticle', $returnid, $gCms->variables['admintheme']->DisplayImage('icons/system/delete.gif', $this->Lang('delete'),'','','systemicon'), array('articleid'=>$row['cgblog_id']), $this->Lang('areyousure'));
  }

  $entryarray[] = $onerow;
    
  ($rowclass=="row1"?$rowclass="row2":$rowclass="row1");
}

$smarty->assign_by_ref('items', $entryarray);
$smarty->assign('itemcount', count($entryarray));

$smarty->assign('addlink', $this->CreateLink($id, 'admin_addarticle', $returnid, $gCms->variables['admintheme']->DisplayImage('icons/system/newobject.gif', $this->Lang('addarticle'),'','','systemicon'), array(), '', false, false, '') .' '. $this->CreateLink($id, 'admin_addarticle', $returnid, $this->Lang('addarticle'), array(), '', false, false, 'class="pageoptions"'));

$smarty->assign('form2start',$this->CreateFormStart($id,'defaultadmin',$returnid));
$smarty->assign('form2end',$this->CreateFormEnd());
$smarty->assign('submit_reassign',$this->CreateInputSubmit($id,'submit_reassign',$this->Lang('submit')));
$categorylist = $this->get_category_list();
$smarty->assign('categoryinput',$this->CreateInputDropdown($id,'category',$categorylist));
if( $this->CheckPermission('Delete CGBlog') ) {
  $smarty->assign('submit_massdelete',
		  $this->CreateInputSubmit($id,'submit_massdelete',$this->Lang('delete_selected'),
					   '','',$this->Lang('areyousure_deletemultiple')));
}

$smarty->assign('selecttext',$this->Lang('select'));
$smarty->assign('filtertext',$this->Lang('title_filter'));
$smarty->assign('statustext',$this->Lang('status'));
$smarty->assign('startdatetext',$this->Lang('startdate'));
$smarty->assign('enddatetext',$this->Lang('enddate'));
$smarty->assign('statetext',$this->Lang('state'));
$smarty->assign('titletext', $this->Lang('title'));
$smarty->assign('postdatetext', $this->Lang('postdate'));
$smarty->assign('authortext',$this->Lang('author'));
$blaster = cms_utils::get_module('CGSocialBlaster');
if( is_object($blaster) ) {
  $smarty->assign('have_blaster',1);
  $smarty->assign('blaster_tag','{cgsocblaster module=CGBlog key=$entry->id}');
}

$config = $this->GetConfig();
$themedir = $config['root_url'].'/'.$config['admin_dir'].'/themes/'.$admintheme->themeName.'/images/icons/system';

$smarty->assign('iconurl',$themedir);

#Display template
echo $this->ProcessTemplate('articlelist.tpl');

// EOF
?>
